﻿import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import QtQuick.Templates as T
import FilUI

T.ComboBox {
    id: control

    property bool disabled                      : false
    property color normalColor                  : Qt.rgba(254/255, 254/255, 254/255, 1)
    property color hoverColor                   : Qt.rgba(251/255, 251/255, 251/255, 1)
    property color disableColor                 : Qt.rgba(252/255, 252/255, 252/255, 1)
    property alias textBox                      : text_field

    signal commit(string text)

    implicitWidth                               : Math.max(implicitBackgroundWidth + leftInset + rightInset,
                                                           implicitContentWidth + leftPadding + rightPadding)
    implicitHeight                              : Math.max(implicitBackgroundHeight + topInset + bottomInset,
                                                           implicitContentHeight + topPadding + bottomPadding,
                                                           implicitIndicatorHeight + topPadding + bottomPadding)
    font                                        : FilTextStyle.Body
    leftPadding                                 : padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
    rightPadding                                : padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
    enabled                                     : !disabled
    delegate                                    : FilItemDelegate {
        width                                   : ListView.view.width
        text                                    : control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
        palette.text                            : control.palette.text
        font                                    : control.font
        palette.highlightedText                 : control.palette.highlightedText
        highlighted                             : control.highlightedIndex === index
        hoverEnabled                            : control.hoverEnabled
    }
    focusPolicy                                 : Qt.TabFocus
    indicator                                   : FilIcon {
        x                                       : control.mirrored ? control.padding : control.width - width - control.padding
        y                                       : control.topPadding + (control.availableHeight - height) / 2
        width                                   : 28
        iconSource                              : FilIcons.ChevronDown
        iconSize                                : 15
        opacity                                 : enabled ? 1 : 0.3
    }
    contentItem                                 : T.TextField {
        id: text_field
        property bool disabled                  : !control.editable
        leftPadding                             : !control.mirrored ? 10 : control.editable && activeFocus ? 3 : 1
        rightPadding                            : control.mirrored ? 10 : control.editable && activeFocus ? 3 : 1
        topPadding                              : 6 - control.padding
        bottomPadding                           : 6 - control.padding
        renderType                              : FilTheme.nativeText ? Text.NativeRendering : Text.QtRendering
        selectionColor                          : FilTools.withOpacity(FilTheme.primaryColor, 0.5)
        selectedTextColor                       : color
        text                                    : control.editable ? control.editText : control.displayText
        enabled                                 : control.editable
        autoScroll                              : control.editable
        font                                    : control.font
        readOnly                                : control.down
        color                                   : {
            if (!control.enabled) {
                return Qt.rgba(160/255, 160/255, 160/255, 1)
            }
            return Qt.rgba(27/255, 27/255, 27/255, 1)
        }
        inputMethodHints                        : control.inputMethodHints
        validator                               : control.validator
        selectByMouse                           : true
        verticalAlignment                       : Text.AlignVCenter
        background                              : FilTextBoxBackground {
            border.width                        : 1
            bottomMargin                        : {
                if (!control.editable) {
                    return 1
                }
                return contentItem && contentItem.activeFocus ? 2 : 1
            }
            inputItem                           : contentItem
        }
        Component.onCompleted                  : {
            forceActiveFocus()
        }
        Keys.onEnterPressed                     : (event) => handleCommit(event)
        Keys.onReturnPressed                    :(event) => handleCommit(event)
        function handleCommit(event) {
            control.commit(control.editText)
            accepted()
        }
    }
    background                                  : Rectangle {
        implicitWidth                           : 140
        implicitHeight                          : 32
        border.color                            : "#DFDFDF"
        border.width                            : 1
        visible                                 : !control.flat || control.down
        radius                                  : 4
        FilFocusRectangle {
            visible                             : control.visualFocus
            radius                              : 4
            anchors.margins                     : -2
        }
        color                                   : {
            if (!enabled) {
                return disableColor
            }
            return hovered ? hoverColor :normalColor
        }
    }
    popup                                       : T.Popup {
        y                                       : control.height
        width                                   : control.width
        height                                  : Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
        topMargin                               : 32
        bottomMargin                            : 6
        modal                                   : false //true
        contentItem                             : ListView {
            clip                                : true
            implicitHeight                      : contentHeight
            model                               : control.delegateModel
            currentIndex                        : control.highlightedIndex
            highlightMoveDuration               : 0
            boundsMovement                      : Flickable.StopAtBounds
            T.ScrollIndicator.vertical          : ScrollIndicator { }
        }
        enter                                   : Transition {
            NumberAnimation {
                property                        : "opacity"
                from                            : 0
                to                              : 1
                duration                        : FilTheme.animationEnabled ? 83 : 0
            }
        }
        exit                                    : Transition {
            NumberAnimation {
                property                        : "opacity"
                from                            : 1
                to                              : 0
                duration                        : FilTheme.animationEnabled ? 83 : 0
            }
        }
        background                              : Rectangle {
            radius                              : 5
            color                               : Qt.rgba(1, 1, 1, 1)
            border.color                        : Qt.rgba(191/255, 191/255, 191/255, 1)
            FilShadow {
                radius                          : 5
            }
        }
    }
}
